var $utils = $('utils').extend({
	myName: "$('$utils')",
	parameters: [],
	log: function(e){
		if($utils.parameters['debug']){
			if(!console.log){//TODO:append body code.
				window.alert(e);
				return;
			}
			console.log(e);
		}
	},
	getParametersFromURL: function(url){
		if (url.match(/https?[^?]+\?([^#]*)/)) {
			var q = RegExp.$1;
			var a = q.split('&')
			for (var n in a) {
				if (a[n].match(/^([^=]+)=(.*)$/)) {
					$utils.parameters[RegExp.$1] = decodeURIComponent(RegExp.$2);
				}
			}
			//finish.
			if($utils.parameters['debug']){
				var s = $utils.myName+'.parameters:[';
				for(var i in $utils.parameters){
					s=s+'"'+i+'",';
				}
				s=s.slice(0,-1)+']';
				console.log(s);
			}
			$utils.log($utils.myName+'.getParametersFromURL():');
		}
	}
});
$(function(){
	$utils.getParametersFromURL(location.href);
});

var $jsonengine = $('jsonengine').extend({
	myName: "$('$jsonengine')",
	log: window.alert,
	docType: '',
	filter: '',
	_ajax: function(success, error){
		$jsonengine.log($jsonengine.method+' '+$jsonengine.uri);
		$.ajax({
			url    : $jsonengine.uri,
			type   : $jsonengine.method,
			success: success,
			error  : error
		});
	},
	_get: function(success, error){
		$jsonengine.uri = '/_je/' + $jsonengine.docType + $jsonengine.filter;
		$jsonengine.method = 'GET';
		$jsonengine._ajax(success, error);
		//finish.
		$jsonengine.log($jsonengine.myName+'._get():');
	},
	_post: function(success, error){
		$jsonengine.uri = '/_je/' + $jsonengine.docType + '?' + $jsonengine.filter;
		$jsonengine.method = 'POST';
		$jsonengine._ajax(success, error);
		//finish.
		$jsonengine.log($jsonengine.myName+'._post():');
	},
	_put: function(success, error){
		$jsonengine.uri = '/_je/' + $jsonengine.docType + '?' + $jsonengine.filter;
		$jsonengine.method = 'PUT';
		$jsonengine._ajax(success, error);
		//finish.
		$jsonengine.log($jsonengine.myName+'._put():');
	},
	_delete: function(success, error){
		$jsonengine.uri = '/_je/' + $jsonengine.docType + '/' + $jsonengine.filter;
		$jsonengine.method = 'DELETE';
		$jsonengine._ajax(success, error);
		//finish.
		$jsonengine.log($jsonengine.myName+'._delete():');
	}
});
$(function(){
	if($utils){
		$jsonengine.log = $utils.log;
		if($utils.parameters['docType'])
			$jsonengine.docType = $utils.parameters['docType'];
	}
});

$(function(){
	//title
	$('title').append('JKL Dumper Test');
	//script
	var head = $('head');
	var js = $('<script type="text/javascript" src="jkl-dumper.js"/>');
	head.append(js);
	//main
	var createList = function(json, status, xhr){
		$jsonengine.log(xhr);
		var dumper = new JKL.Dumper();          // Dumperオブジェクトを生成
		var s = dumper.dump(json);
		$jsonengine.log(s);
		$('body').append(s);  // 変数の内容を出力する
	};
	var errorMessage = function(xhr, status){
		$jsonengine.log(xhr);
	};

	$jsonengine._get(createList, errorMessage);
	var data = {
		string: "string",
		array:  [ 1, 2, 3 ],
		hash:   { key1: "value1", key2: "value2" },
		data1:  null,
		data2:  true,
		data3:  false
	};
});
